/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function(){
	$(".category-index").on('click','.cate-delete',function(){
	    var cate_id = $(this).attr('data-id');		
		layer.confirm('您确定要删除该条数据', {btn: ['删除', '取消' ]}, function(index, layero){
		    $params = JSON.stringify({'id':cate_id});
		    $delete_back = ajaxSynchroPostRequest("/admincategory/category/delete",$params);
		    if($delete_back.success==1){
				$("#cate_"+cate_id).remove();
		        layer.msg($delete_back.msg);
		        layer.close(index);
		        window.location.reload();
		    }
			    else{
			        layer.msg($delete_back.msg);
			    }
		});
	});
	
    $(".category-index").on('click','.cate-update',function(){
        var cate_id = $(this).attr('data-id');
        $params = JSON.stringify({'id':cate_id});
        $update_init_back = ajaxSynchroPostRequest("/admincategory/category/update",$params);
        $update_init_back.data.name;
        contentHtml = '<div class="p30 layui-form"><table ><tr><td class="item-label">分类名称：</td><td class="item-val"><input type="text" id="cate-name-update-'+cate_id+'" value="'+$update_init_back.data.name+'" class="layui-input"/></td></tr></table></div>';

        $cate_update_view = layer.open({
            type: 1,
			title:'修改 <b>'+($update_init_back.data.name)+'</b>',
            content: contentHtml,
            btn: ['提交', '取消'],
            btnAlign: 'c',
			yes: function(index, layero){
			    var name = $("#cate-name-update-"+cate_id).val();
			    $params = JSON.stringify({'id':cate_id,'name':name});
			    $update_back = ajaxSynchroPostRequest("/admincategory/category/update",$params);
			    console.log($update_back);
			    if($update_back.success===1){
			        $("#cate-name-"+cate_id).html(name);
					layer.msg($update_back.msg);
					layer.close($cate_update_view);
					window.location.reload();
			    }
			    else{
			        layer.msg($update_back.msg);
			    }
				
			  }
          });
    });
    //创建下级分类
    $(".category-index").on('click','.add-subcate-button',function(){
        var cate_id = $(this).attr('data-id');
        var grade = $(this).attr('data-grade');
        contentHtml = '<div class="p30 layui-form"><table><tr><td class="item-label">分类名称：</td><td class="item-val"><input type="text" id="cate-name-create" value="" class="layui-input"/></td></tr></table></div>';

        $cate_create_view = layer.open({
            type: 1,
			title:'创建分类',
            content: contentHtml,
            btn: ['提交', '取消'],
            btnAlign: 'c',
            yes: function(index, layero){
                var name = $("#cate-name-create").val();
                $params = JSON.stringify({'parent':cate_id,'name':name,'grade':grade});
                $create_back = ajaxSynchroPostRequest("/admincategory/category/create",$params);
                console.log($create_back);
				if($create_back.success===1){
					layer.msg($create_back.msg);
					window.location.reload();
				}
				else{
				    layer.msg($create_back.msg);
				}
              }
          });
    });
});

